import configs from './config.js'

const baseUrl = configs.ApiUrl1
const service = (api = '', date = {}, type = 'GET', header = {}) => {
	
	return new Promise((resolve, reject) => {
	var	 tokens =''
		if(uni.getStorageSync('tokens')){
		tokens='Bearer '+uni.getStorageSync('tokens')
		}
		uni.request({
			method: api.method,
			
			url: baseUrl + api.url,
			data: api.data,
			
		header: {
			'Content-Type':'application/json',
			'Authorization':tokens
		},
			dataType: 'json',
		}).then((response) => {
			setTimeout(function() {
				uni.hideLoading();
			}, 500);
			let [error, res] = response;
			if (error == null) {
				if (res.data.code == 20000) {
					
					resolve(res.data);
					
				}else if (res.data.code == 404||res.data.code == 'black') {
					
				uni.redirectTo({
					url:'/pages/404/404'
				})
				
					
				}else {
				uni.showToast({
					title:res.data.message,
					icon: 'none', //将值设置为 success 或者 ''
					duration: 2000 //持续时间为 2秒
				})
				reject(error)
							
				}
			} else {
				
				uni.showToast({
					title:res.data.message,
					icon: 'none', //将值设置为 success 或者 ''
					duration: 2000 //持续时间为 2秒
				})
				reject(error)
			}





		})

	});
}
export default service